
from ctypes import c_int,c_bool,c_float
import numpy as np
import atb
from glfw import *
from gl_utils import adjust_gl_view, draw_gl_texture, clear_gl_screen,draw_gl_point,draw_gl_point_norm,draw_gl_polyline
from time import time
import OpenGL.GL as gl

def main():
    # # callback functions
    def on_resize(w, h):
        atb.TwWindowSize(w, h);
        adjust_gl_view(w,h)

    def on_key(key, pressed):
        if not atb.TwEventKeyboardGLFW(key,pressed):
            if pressed:
                if key == GLFW_KEY_ESC:
                    on_close()

    def on_char(char, pressed):
        if not atb.TwEventCharGLFW(char,pressed):
            pass

    def on_button(button, pressed):
        if not atb.TwEventMouseButtonGLFW(button,pressed):
            pass

    def on_pos(x, y):
        if atb.TwMouseMotion(x,y):
            pass


    def on_scroll(pos):
        if not atb.TwMouseWheel(pos):
            pass

    def on_close():
        quit.value = True
        print "Process closing from window"


    width,height = 1280,720

    ### any ATB varibles need to be c_types
    quit = c_bool(0)
    t_stamp = time()
    fps = c_float(0)
    point_size = c_int(1)


    def set_point_size(size):
        point_size.value = size
        gl.glPointSize(point_size.value)

    def get_point_size():
        return point_size.value




    atb.init()
    bar = atb.Bar(name="ATB_Bar", label="Controls",
        help="Sample Bar", color=(50,50,50), alpha=100,
        text='light',position=(10, 10),refresh=.1, size=(200, 200))

    bar.add_var("FPS", fps, step=1., readonly=True)
    bar.add_var("Point Size",point_size, setter=set_point_size, getter=get_point_size, min=1,step=1)
    bar.add_var("Exit",quit)


    # Initialize glfw
    glfwInit()
    glfwOpenWindow(width, height, 0, 0, 0, 8, 0, 0, GLFW_WINDOW)
    glfwSetWindowTitle("GLFW Window")
    glfwSetWindowPos(20,20)
    # glfwSwapInterval(0) # turn of v-sync when using video as src for benchmarking, else fps will be capped at 60hz


    #register callbacks
    glfwSetWindowSizeCallback(on_resize)
    glfwSetWindowCloseCallback(on_close)
    glfwSetKeyCallback(on_key)
    glfwSetCharCallback(on_char)
    glfwSetMouseButtonCallback(on_button)
    glfwSetMousePosCallback(on_pos)
    glfwSetMouseWheelCallback(on_scroll)

    #gl_state settings
    gl.glEnable(gl.GL_POINT_SMOOTH)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    gl.glEnable(gl.GL_BLEND)


    #event loop
    while glfwGetWindowParam(GLFW_OPENED) and not quit.value:
        ### calculate fps
        new_time = time()
        fps.value = 1/(new_time - t_stamp)
        t_stamp = new_time

        ###you can write to pixels by putting stuff in here...
        # img = np.empty((height,width,3),dtype=np.uint8)

        # make some RGB noise
        img = np.random.random((2,3,4))*255
        img2 = np.random.random((height,width,3))*255

        ### GL-drawing
        clear_gl_screen()
        draw_gl_texture(img2)
        draw_gl_texture(img)

        ###draw a point
        draw_gl_point_norm((0,0),(1.,0.,0.,0.5))

        atb.draw()
        glfwSwapBuffers()


    #end while running

    print "Process closed"
    atb.terminate()
    glfwCloseWindow()
    glfwTerminate()


if __name__ == '__main__':
    import cProfile
    cProfile.runctx("main()",{},locals(),"main.pstats")
    import subprocess
    subprocess.call("python gprof2dot.py -f pstats main.pstats | dot -Tpng -o benchmark.png", shell=True)
    # main()
